package pandabay2.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pandabay2.DAO.AuctionDAO;
import pandabay2.domain.Auction;
import pandabay2.service.ProductSearch;

public class SearchServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String searchString = request.getParameter("searchString");
		
		ProductSearch ps = new ProductSearch();
		List searchResults = ps.searchProducts(searchString);
		
		request.setAttribute("searchResults", searchResults);
		RequestDispatcher view = request.getRequestDispatcher("searchResult.jsp");
		
		view.forward(request, response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			System.out.println("In the Search Servlet");
			List<Auction> auctions;
			String tmpCategory = request.getParameter("category");
			String searchString = request.getParameter("searchString");
			if(tmpCategory.equals("Brettspiel"))
				auctions = AuctionDAO.findString(searchString, "Brettspiel");
			else if(tmpCategory.equals("Stofftier"))
				auctions = AuctionDAO.findString(searchString, "Stofftier");
			else if(tmpCategory.equals("Kinderbuch"))
				auctions = AuctionDAO.findString(searchString, "Kinderbuch");
			else {
				auctions = AuctionDAO.findString(searchString, "");
			}
			request.setAttribute("auctions", auctions);
			request.getRequestDispatcher("/searchResult.jsp").forward(request, response);
		} catch (Throwable exc) {
			System.out.println(exc);
		}
	}

}